package com.jimmy.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jimmy.reggie.common.CustomException;
import com.jimmy.reggie.entity.*;
import com.jimmy.reggie.mapper.SetmealMapper;
import com.jimmy.reggie.service.CategoryService;
import com.jimmy.reggie.service.SetmealDishService;
import com.jimmy.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private CategoryService categoryService;
    /**具体逻辑:
     A. 保存套餐基本信息
     B. 获取套餐关联的菜品集合，并为集合中的每一个元素赋值套餐ID(setmealId)
     C. 批量保存套餐关联的菜品集合
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        this.save(setmealDto);
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes=dishes.stream().map((item)->{
            //注意这里的字段名称，否则会出现报错
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }

    /**
     * 批量删除
     * A. 查询该批次套餐中是否存在售卖中的套餐, 如果存在, 不允许删除
     * B. 删除套餐数据
     * C. 删除套餐关联的菜品数据
     * @param ids
     */
    @Override
    public void removeWithDish(List<Long> ids) {
          //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态，确定是否可用删除
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);
        int count = this.count(setmealLambdaQueryWrapper);
        if (count>0){
            throw new CustomException("套餐正在售卖中，无法进行删除操作");
        }
        this.removeByIds(ids);
        //删除关联数据
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
    }

    @Override
    public SetmealDto getByIdWithAll(Long id) {
        Setmeal setmeal= this.getById(id);
        Long categoryId = setmeal.getCategoryId();
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(id!=null,SetmealDish::getSetmealId,id);
        List<SetmealDish> dishlist = setmealDishService.list(setmealDishLambdaQueryWrapper);
        Category category = categoryService.getById(categoryId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        setmealDto.setCategoryName(category.getName());
        setmealDto.setSetmealDishes(dishlist);
        return setmealDto;
    }

    @Override
    public void UpdateWithDishes(SetmealDto setmealDto) {
        //1.先把除了口味以外的信息存储了，然后第二个就是清楚对应id的口味信息，然后重新赋值
        this.updateById(setmealDto);
        //2.清除口味信息
        LambdaQueryWrapper<SetmealDish> setmealDishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealDishLambdaQueryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(setmealDishLambdaQueryWrapper);
        //3. 现在开始存储菜品的信息
        List<SetmealDish> dishes = setmealDto.getSetmealDishes();
        dishes=dishes.stream().map((item)->{
              item.setSetmealId(setmealDto.getId());
              return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(dishes);
    }
}
